<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    clear: left;
    /* margin-top: 15px; */
  }
  .sms-send-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .confirm-send {
    float: left;
    margin-bottom: 10px;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>批量发送</span>
    </div>
    <div class="line"></div>
    <sms-send-form @on-success-valid="handleSubmit" class="sms-send-form"></sms-send-form>
    <Button
      icon="md-send"
      type="primary"
      class="confirm-send"
      @click="handleClick">
      确认发送
    </Button>
    <sms-send-drawer
      @sms-send-valid="handleEdit">
    </sms-send-drawer>
    <sms-send-confirm
      :confirmData="confirmData"
      @confirm-valid="handleConfirm">
    </sms-send-confirm>
    <Table
      class="table"
      :columns="columns"
      :data="historyData"
      :loading="loading"
      border
      @on-select="handleSelect"
      @on-select-cancel="handleCancelSelect"
      @on-select-all="handleSelectAll"
      @on-select-all-cancel="handleCancelAll">
    </Table>
    <table-page
      class="paging"
      :dataCount="dataCount"
      @change-page="changePage"
      @change-page-size="changePageSize"
      :pageSize="pageSize">
    </table-page>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import TablePage from '_c/table-page'
import SmsSendDrawer from '_c/sms-send-drawer'
import smsSendForm from '_c/sms-send-form'
import SmsSendConfirm from '_c/sms-send-confirm'
export default {
  components: {
    SmsSendDrawer,
    smsSendForm,
    SmsSendConfirm,
    TablePage
  },
  data () {
    return {
      page: 1,
      title: '短信管理批量发送',
      confirmData: {},
      selectData: [],
      postData: {},
      postid: [],
      playload: null,
      rows: 20,
      index: '',
      loading: true,
      columns: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '序号',
          type: 'index2',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '跟踪状态',
          key: 'state0',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state0 === 0 ? '有效' : '流失')
          }
        },
        {
          title: '客户状态',
          key: 'state',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.state === 0 ? '未预约' : params.row.state === 1 ? '已预约' : params.row.state === 2 ? '未到访' : params.row.state === 3 ? '已到店' : params.row.state === 4 ? '已签约' : params.row.state === 5 ? '已退款' : '-')
          }
        },
        {
          title: '姓名',
          key: 'uname',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '电话',
          key: 'tel',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '加入日期',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '内部销售姓名',
          key: 'mname',
          minWidth: 200,
          align: 'center'
        },
        {
          title: '签约金额',
          key: 'cprice',
          minWidth: 150,
          align: 'center',
          render: (h, params) => {
            return h('span', params.row.cprice)
          }
        }
        // {
        //   title: '操作',
        //   key: 'cz',
        //   // width: 196,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('div', [
        //       h('Button', {
        //         props: {
        //           type: 'primary',
        //           size: 'small',
        //           ghost: 'ghost'
        //         },
        //         style: {
        //           marginRight: '5px'
        //         },
        //         on: {
        //           click: () => {
        //             this.handleClick(params.row.id, params.row)
        //           }
        //         }
        //       }, '编辑'),
        //       h('Button', {
        //         props: {
        //           type: 'error',
        //           size: 'small',
        //           ghost: 'ghost'
        //         },
        //         style: {
        //           marginRight: '5px'
        //         },
        //         on: {
        //           click: () => {
        //             this.handleDelete(params.index)
        //           }
        //         }
        //       }, '删除')
        //     ])
        //   }
        // }
      ]
    }
  },
  computed: {
    ...mapState({
      smsSendList: state => state.smsSend.smsSendList,
      dataCount: state => state.smsSend.smsSendDataCount,
      historyData: state => state.smsSend.smsSendHistoryData,
      pageSize: state => state.smsSend.smsSendPageSize
    })
  },
  methods: {
    ...mapActions([
      'getSmsSendList',
      'getNameList',
      'getSmsTemplateList',
      'sendSmsSendData',
      'updateCustomerDrawerState',
      'updateConfirmModalState',
      'changeSmsSendPage',
      'changeSmsSendPageSize'
    ]),
    handleSubmit (playload) {
      this.playload = playload
      this.loading = true
      // console.log(playload)
      this.getSmsSendList({
        ...playload,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    handleClick () {
      this.updateCustomerDrawerState(true)
    },
    handleEdit (playload, id) {
      // console.log(playload)
      this.confirmData = playload
      this.updateCustomerDrawerState(false)
      this.updateConfirmModalState(true)
    },
    async handleConfirm () {
      this.selectData.map((item) => {
        this.postid.push(item.id)
      })
      this.postid = this.postid.join()
      if (this.selectData.length > 1) {
        this.postData = {
          smsid: this.confirmData.id,
          name: this.confirmData.tag,
          roleid: 0,
          users: this.postid
        }
      } else {
        this.postData = {
          smsid: this.confirmData.id,
          name: this.confirmData.tag,
          roleid: this.selectData[0].id
        }
      }
      // console.log(this.postData)
      const resp = await this.sendSmsSendData(this.postData)
      if (resp.data.state === '000') {
        this.$Message.success('发送成功')
      } else {
        this.$Message.error('发送失败')
      }
      this.updateConfirmModalState(false)
    },
    handleSelect (selection, row, params) {
      this.selectData.push(row)
      this.smsSendList[row.index]._checked = true
      row._checked = true
    },
    handleCancelSelect (selection, row) {
      this.selectData.splice(row, 1)
      this.smsSendList[row.index]._checked = false
      row._checked = false
    },
    handleSelectAll (selection) {
      selection.map((item) => {
        this.selectData.push(item)
        this.smsSendList[item.index]._checked = true
        item._checked = true
      })
    },
    handleCancelAll (selection) {
      for (let i = this.pageSize * (this.page - 1); i < this.pageSize * (this.page - 1) + 20; i++) {
        this.smsSendList[i]._checked = false
      }
      this.selectData.splice(this.page > 1 ? this.pageSize * (this.page - 1) - 1 : 0, this.pageSize)
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeSmsSendPage(index)
      this.getSmsSendList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeSmsSendPageSize(value)
      this.changePage(this.page)
      this.getSmsSendList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getSmsSendList({}).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
    this.getSmsTemplateList({
      state: 1
    })
    this.getNameList({})
  }
}
</script>
